using System;
using System.Collections.Generic;
namespace Biomedica.Propability.StateMachine
{
  

    public class State : IStateActions
    {

        public String Name { set; get; }
        public int Id { set; get; }
        public String Text { set; get; }

        public List<Triggers> TriggerList { set; get; }	

		public State(String Name) {

            this.Name = Name;
            TriggerList = new List<Triggers>();
            OnEnter += OnOnEnter;
            OnDo += OnDoState;
            OnExit += OnExitState;



            
		}
        public State(String Name,OnEnterCallback onEnter)
        {
            this.Name = Name;
            TriggerList = new List<Triggers>();
            OnEnter += onEnter;


        }
        public State(String Name, OnEnterCallback onEnter,OnDoCallback onDo)
        {
            this.Name = Name;
            TriggerList = new List<Triggers>();
            OnEnter += onEnter;
            OnDo += onDo;


        }
        public State(String Name, OnEnterCallback onEnter, OnDoCallback onDo, OnExitCallback onExit)
        {
            this.Name = Name;
            TriggerList = new List<Triggers>();
            OnEnter += onEnter;
            OnDo += onDo;
            OnExit += onExit;


        }

		public void Add(Triggers t) {
            TriggerList.Add(t);
		}
		public void Test(StateContext context) {
            try
            {
                OnDo(context);
                foreach (Triggers tr in TriggerList)
                {
                    if (tr.IsValid(context)) 
                    {
                        OnExit(context);
                        context.getStateMachineEngine().CurrentState = context.getStateMachineEngine().StateCollection[tr.Nextstate];
                        context.getStateMachineEngine().CurrentState.OnEnter(context);
                        return;
                    }
                }
                
                
            }
           catch(Exception err)
            {
                throw new StateActionException(Name, err);
            }
            
           
		}
       
        public virtual void OnDoState(StateContext context)
        {

        }
        public virtual void OnOnEnter(StateContext context)
        {

        }
        public virtual void OnExitState(StateContext context)
        {

        }

        public OnDoCallback OnDo
        {
            get;
            set;
        }
        public OnEnterCallback OnEnter
        {
            get;
            set;
        }
        public OnExitCallback OnExit
        {
            get;
            set;
        }

        public void SetTrantionCallback(String toState,OnTranstion e)
        {
            for (int i = 0; i < TriggerList.Count; i++)
            {
                if(TriggerList[i].Nextstate.Equals(toState))  TriggerList[i].OnTranstion = e;
            }
        }

        public void SetTrantionCallback(string toState, OnTranstion e, string TriggerParameter)
        {
            for (int i = 0; i < TriggerList.Count; i++)
            {
                if (TriggerList[i].Nextstate.Equals(toState))
                {
                    TriggerList[i].Parameter = TriggerParameter;
                    TriggerList[i].OnTranstion = e;
                }
            }
        }
    }

}
